package com.ygnn.common.R;

import com.ygnn.exception.BaseErrorInfoInterface;
import lombok.Data;

import java.io.Serializable;

/**
 * ClassName:Result
 * Package:com.ygnn.common.R
 * Description:
 *
 * @Author: dlh
 * @Creat:2024/10/8-22:25
 */

@Data
public class Result<T> implements Serializable {

    private Integer code;   //编码 (1成功, 0失败)
    private String msg; //错误信息
    private T data; //数据

    /**
     * 成功反馈 (无数据)
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(){
        Result<T> result = new Result<>();
        result.setCode(1);
        return result;
    }

    /**
     * 成功反馈 (带数据)
     * @param object
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(T object){
        Result<T> result = new Result<>();
        result.setCode(1);
        result.setData(object);
        return result;
    }

    /**
     * 失败反馈
     * @param msg
     * @return
     * @param <T>
     */
    public static <T> Result<T> error(String msg){
        Result<T> result = new Result<>();
        result.setCode(0);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> error(BaseErrorInfoInterface errorInfo) {
        Result<T> result = new Result<>();
        result.setCode(Integer.valueOf(errorInfo.getResultCode()));
        result.setMsg(errorInfo.getResultMsg());
        result.setData(null);
        return result;
    }

    /**
     * 失败
     */
    public static <T> Result<T> error(String code, String message) {
        Result<T> result = new Result<>();
        result.setCode(Integer.valueOf(code));
        result.setMsg(message);
        result.setData(null);
        return result;
    }


}
